<?php
class CategoryModule
{
	function index() {
		global $_G;
		$id = empty($_REQUEST['id']) ? getGP('url','G') : intval($_REQUEST['id']);
		$cates = $_G['cache']->get('category');
        if ($id)
        {	
        	if(getGP('alias')){
        		$_SERVER['QUERY_STRING'] = 'category.php?id='.$id;
        	}
        	$_G['title'] = $cates[$id]['catename'];
        	if($cates[$id]['keywords'])$_G['keywords'] = $cates[$id]['keywords'];
        	if($cates[$id]['description'])$_G['description'] = $cates[$id]['description'];
        	include template('article');
        	exit;
	        $type= $cates[$id]['type'];//  $_G['db']->result("SELECT type FROM ".DB_PREFIX."category where cid=".$id);
	        $now = $_G['timestamp'];
			switch($type) {
				case '0'://文章
					$limit = 18;
					$articles = array();
					$sql = "SELECT a.aid,a.dateline,c.cid,a.uid,a.username,a.title,a.default_image,a.summary,a.comments,a.views,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND c.cid= '{$id}' AND c.status=0 AND a.ischeck=1 AND a.dateline < $now ORDER BY a.aid DESC ";
					$query = $_G['db']->page_query($sql,$limit);
					$num = $_G['db']->result("SELECT count(*) as num FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND c.cid= '{$id}' AND c.status=0 AND a.ischeck=1 AND a.dateline < $now ORDER BY a.aid DESC ");
					while($data = $_G['db']->fetch_array($query)) {
						$articles[] = $data ;
					}
					$class['cate'.$id]=1;
					$pager = pager($num,$limit);
					include template('article');
				break;
				case '1'://网址链接
					$limit = 15;
					$links = array();
					$sql = "SELECT l.*,c.catename,u.username FROM ".DB_PREFIX."link l LEFT JOIN ".DB_PREFIX."category c ON l.cid=c.cid LEFT JOIN ".DB_PREFIX."users u ON l.uid=u.uid WHERE c.type=1 AND c.cid= '{$id}' AND l.visible=1 AND c.status=0 ORDER BY l.listorder DESC ";
					$query = $_G['db']->page_query($sql,$limit);
					$num = $_G['db']->result("SELECT count(*) as num FROM ".DB_PREFIX."link l LEFT JOIN ".DB_PREFIX."category c ON l.cid=c.cid LEFT JOIN ".DB_PREFIX."users u ON l.uid=u.uid WHERE c.type=1 AND c.cid= '{$id}' AND l.visible=1 AND c.status=0 ORDER BY l.listorder DESC ");
					while($data = $_G['db']->fetch_array($query)) {
						$links[] = $data ;
					}
					$pager = pager($num,$limit);
					include template('link');
				default:
	
				break;
			}
	        
        } else {
	        $article = $_G['cache']->get($id,'article');
	        $comment = $_G['cache']->get($id,'comment');
	        if( !$article) {
	        	$article = $_G['db']->fetch('article','*',array('aid'=>$id));
	        	$_G['cache']->set($id,$article,'article');
	        }
	        if(!$comment) {
	        	$comment = $_G['db']->fetch('comment','*',array('referid'=>$id,'type'=>0));
	        	$_G['cache']->set($id,$comment,'comment');
	        }
	        
        }
		
	}
}	